const path = require('path');
const CompressionWebpackPlugin = require('compression-webpack-plugin')
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const productionGzipExtensions = ['js', 'css']
const Timestamp = new Date().getTime();
module.exports = {
    publicPath: "",
    outputDir: `wxBackend`,
    // 关闭线上源码
    productionSourceMap: false,
    chainWebpack: (config) => {
        config.resolve
        .set("symlinks", false) // https://github.com/vuejs/vue-cli/issues/2675
        .extensions.merge([".js", ".jsx", ".vue", ".json",'env']);
          // 移除 prefetch 插件
        config.plugins.delete('prefetch');
        // 移除 preload 插件
        config.plugins.delete('preload');
        config.resolve.alias
        .set("@", path.resolve("src"))
        .set('api', path.resolve('src/api'))
        .set("images", path.resolve("src/assets/image"))
        .set("components", path.resolve("src/components"));
        const svgRule = config.module.rule('svg');
     
        svgRule.uses.clear();
     
        svgRule
          .use('babel-loader')
          .loader('babel-loader')
          .end()
      },
      css:{
        // 是否使用css分离插件 ExtractTextPlugin
         // extract: false,
         // 开启 CSS source maps
         sourceMap: false,
         // css预设器配置项
         loaderOptions: {
         }
     },
     configureWebpack:(config) => {
      if(process.env.NODE_ENV != 'development'){
        // const Uglify = new UglifyJsPlugin({
        //     uglifyOptions: {
        //         compress: {
        //           drop_debugger: true,
        //           drop_console: true,
        //         },
        //         warnings: false ,
        //     },
        //     sourceMap: false,
        //     parallel: true,
        // })
        const Compression = new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        })
        config.plugins = [...config.plugins,Compression]
        config.output.filename = `js/[name].[hash:8].${Timestamp}.js`;
        config.output.chunkFilename = `js/[name].[hash:8].${Timestamp}.js`
      }
    },
      // 反向代理
    devServer: {
        // 环境配置
        port: process.env.VUE_APP_Port || "3839",
        disableHostCheck: true, // 本地hosts配域名调试用
        overlay: {
          warnings: true,
          errors: true
        },
        https: false,
        hotOnly: false,
        // // 配置自动启动浏览器
        open: false,
        proxy: {
          "/api": {
            target: 'http://www.xiangmx.top',
            ws: false,
            // changeOrigin:true,
            pathRewrite: {
              '^/api': '/api'
            }
          },
        },
      },
      pluginOptions: {
        'style-resources-loader': {
          preProcessor: 'less',
          patterns: [
            path.resolve(__dirname, `src/main.less`)
          ]
        }
      }
}